/*
 * COPYRIGHT. ShenZhen iByte Technology Co., Ltd. 2018.
 * ALL RIGHTS RESERVED.
 *
 * No part of this publication may be reproduced, stored in a retrieval system, or transmitted,
 * on any form or by any means, electronic, mechanical, photocopying, recording, 
 * or otherwise, without the prior written permission of ShenZhen iByte Network Technology Co., Ltd.
 *
 * Amendment History:
 * 
 * Date                   By              Description
 * -------------------    -----------     -------------------------------------------
 * Nov 30, 2018    Li.shangzhi         Create the class
 */
package com.github.icloud.elasticsearch.api.docment;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.junit.Test;
import com.github.icloud.elasticsearch.ElasticsearchClientBase;
/**
 * @FileName BulkAPI.java
 * @Description: Elasticsearch 批处理 API
 *
 * @Date Nov 30, 2018 1:39:12 PM
 * @author Li.shangzhi
 * @version 1.0
 */
public class BulkAPI extends ElasticsearchClientBase{

	@Test
	public void iEsPrepareBulk() throws IOException {
		BulkRequestBuilder bulkRequset  =client.prepareBulk();
		bulkRequset.add(client.prepareIndex("icloud-iot-log", "icloud-auth",UUID.randomUUID().toString().replace("-", "").toLowerCase())
				.setSource(jsonBuilder()
						.startObject()
						.field("user", "lishangzhi")
						.field("age",25)
						.field("postDate", new Date())
						.field("message", "trying out ....2")
						.endObject()
						)
				);
		BulkResponse bulkResponse = bulkRequset.get();
		if(bulkResponse.hasFailures()){
			System.out.println("批量写入失败！");
		}else{
			System.out.println("批量写入成功！"); 
		}
	}
}
